package com.sinoglobal.dumping.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.sinoglobal.dumping.activity.Dumpling_MainActivity;
import com.sinoglobal.dumping.activity.Dumpling_ReceiveMoneyActivity;
import com.sinoglobal.dumping.api.Dumpling_RemoteImpl;
import com.sinoglobal.dumping.base.Dumpling_SinoAsyncTask;
import com.sinoglobal.dumping.base.Dumpling_SinoConfig;
import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.base.Dumpling_SinoValueManager;
import com.sinoglobal.dumping.bean.Dumpling_BaseVo;
import com.sinoglobal.dumping.bean.Dumpling_LogOutGetMoneyBean;
import com.sinoglobal.dumping.bean.Dumpling_ShareInfo;

/**
 * Created by lxy on 2015/11/10.
 */
public class Dumpling_HttpMethodSet {
    public static boolean isGetMoney;
    public static boolean isGetSucceed = true;
    public static OnGetMoneyListener onGetMoneyListener;

    public static final Dumpling_SinoAsyncTask addAcquireDunmplingNum(final Context context, final String userId) {
        return new Dumpling_SinoAsyncTask<Object, Void, Dumpling_BaseVo>(context, true, true) {

            @Override
            public Dumpling_BaseVo before(Object... params) throws Exception {
                int num = (Integer) Dumpling_SinoValueManager.getValue(context, Dumpling_SinoConstans.DUMPLING_NO_LOGIN_COUNT, 0);
                if (num > 3) {
                    num = 0;
                }
                return Dumpling_RemoteImpl.getInstance().addAcquireDumplingNum(userId, context, num);
            }

            @Override
            public void after(Dumpling_BaseVo baseVo) {
                if (baseVo != null) {
                    if (Dumpling_SinoConstans.SUCCESS_CODE == baseVo.getCode()) {
                    }
                }
            }

            @Override
            public void exception() {

            }
        };
    }

    public static final void markShare(final Context context, final String userId) {
        new Dumpling_SinoAsyncTask<Void, Void, Dumpling_BaseVo>(context, false) {

            @Override
            public Dumpling_BaseVo before(Void... params) throws Exception {
                return Dumpling_RemoteImpl.getInstance().markShareNum(userId);
            }

            @Override
            public void after(Dumpling_BaseVo baseVo) {
                if (baseVo != null) {
                    if (baseVo.getCode() == 100) {
                    }
                }
            }

            @Override
            public void exception() {
            }
        }.execute();
    }

    public static final void getShareContent(final Context context, final String temp_id, final String picurl, final String starName, final String prizeMoney, final String allMoney) {
        new Dumpling_SinoAsyncTask<Void, Void, Dumpling_ShareInfo>(context, false, false) {
            @Override
            public Dumpling_ShareInfo before(Void... params) throws Exception {
                return Dumpling_RemoteImpl.getInstance().getShareInfo(temp_id, picurl, starName, prizeMoney, allMoney);
            }

            @Override
            public void after(Dumpling_ShareInfo shareInfo) {
                if (shareInfo != null) {
                    if (shareInfo.getCode() == 100) {
                        if (temp_id.equals(Dumpling_MainActivity.TEMP_ID_EVENT)) {
                            Dumpling_SinoConfig.sEventShare = shareInfo;    // 
                        } else if (temp_id.equals(Dumpling_MainActivity.TEMP_ID_MONEY)) {
                            Dumpling_SinoConfig.sMoneyShare = shareInfo;
                        } else if (temp_id.equals(Dumpling_MainActivity.TEMP_ID_STRUT)) {
                            Dumpling_SinoConfig.sStrutShare = shareInfo;
                        } else if (temp_id.equals(Dumpling_MainActivity.TEMP_ID_MAKE)) {
                            Dumpling_SinoConfig.sMakeShare = shareInfo;
                        } else if (temp_id.equals(Dumpling_MainActivity.TEMP_ID_MAKE_EVENT)) {
                            Dumpling_SinoConfig.sMakeEventShare = shareInfo;
                        } else if (temp_id.equals(Dumpling_MainActivity.TEMP_ID_COUPON)) {
                            Dumpling_SinoConfig.sCouponShare = shareInfo;
                        }else if (temp_id.equals(Dumpling_MainActivity.TEMP_ID_CARD)) {
                            Dumpling_SinoConfig.sCardShare = shareInfo;
                        }
                    }
                }

            }

            @Override
            public void exception() {

            }
        }.execute();
    }

    public static void logoutUserGetMoney(final Context context) {
        final String id = (String) Dumpling_SinoValueManager.getValue(context, Dumpling_SinoConstans.DUMPLING_ID_LIST, "");
        if (TextUtils.isEmpty(id)) {
            return;
        }
        new Dumpling_SinoAsyncTask<Void, Void, Dumpling_LogOutGetMoneyBean>(context, false, false) {

            @Override
            public Dumpling_LogOutGetMoneyBean before(Void... params) throws Exception {
                return Dumpling_RemoteImpl.getInstance().logoutGetMoney(id);
            }

            @Override
            public void after(Dumpling_LogOutGetMoneyBean logOutGetMoneyBean) {
                if (logOutGetMoneyBean != null) {
                    Dumpling_SinoValueManager.putValue(context,Dumpling_SinoConstans.DUMPLING_CODE,logOutGetMoneyBean.getCode(),true);
                    if (logOutGetMoneyBean.getCode() != Dumpling_SinoConstans.SUCCESS_CODE) {
                        isGetSucceed = false;
                        if (isGetMoney) {
                            Toast.makeText(context, logOutGetMoneyBean.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        isGetSucceed = true;
                        Toast.makeText(context, logOutGetMoneyBean.getMessage(), Toast.LENGTH_SHORT).show();
                        if (isGetMoney) {
                            Intent intent = new Intent();
                            intent.setClass(context, Dumpling_ReceiveMoneyActivity.class);
                            Dumpling_SinoValueManager.removeValue(context, Dumpling_SinoConstans.DUMPLING_ID_LIST, true);
                            Dumpling_SinoValueManager.removeValue(context, Dumpling_SinoConstans.DUMPLING_MONEY, true);
                            Dumpling_SinoValueManager.removeValue(context,Dumpling_SinoConstans.DUMPLING_COUPON,true);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            isGetMoney = !isGetMoney;
                            context.startActivity(intent);
                        }
                    }
                    if (onGetMoneyListener != null) {
                        onGetMoneyListener.onGetMoney(logOutGetMoneyBean.getCode());
                    }
                }
            }

            @Override
            public void exception() {
                try {
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(context, "饺子领取失败，请检查您的网络~", Toast.LENGTH_SHORT).show();
                            if (onGetMoneyListener != null) {
                                onGetMoneyListener.onGetMoney(999);
                            }

                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }.execute();
    }

    public static void setOnGetMoneyListener(OnGetMoneyListener listener) {
        Dumpling_HttpMethodSet.onGetMoneyListener = listener;
    }

    public interface OnGetMoneyListener {
        void onGetMoney(int code);
    }

    /**
     * 判断平台 请求完成任务接口
     *
     * @param context
     * @param pannikinId
     */
    public static void changeStatus(Context context, final String pannikinId) {
        new Dumpling_SinoAsyncTask<Object, Void, Dumpling_BaseVo>(context, false, false) {

            @Override
            public Dumpling_BaseVo before(Object... params) throws Exception {
                return Dumpling_RemoteImpl.getInstance().changeStatus(pannikinId);
            }

            @Override
            public void after(Dumpling_BaseVo dumpling_baseVo) {
            }

            @Override
            public void exception() {

            }
        }.execute();
    }


}
